using UnityEngine;

public class PlayerAIState_Offense_GrabRebound : State<PlayerAI>
{
	public static PlayerAIState_Offense_GrabRebound instance;

	private PlayerAIState_Offense_GrabRebound()
	{
	}

	public static PlayerAIState_Offense_GrabRebound getInstance()
	{
		if (instance == null)
		{
			instance = new PlayerAIState_Offense_GrabRebound();
		}
		return instance;
	}

	public override void fixedUpdate(PlayerAI owner)
	{
		base.fixedUpdate(owner);
		if (owner.time_Stop > 0f)
		{
			owner.time_Stop -= Time.fixedDeltaTime;
			owner.player.do_idle();
		}
		else if (owner.player.isInState(PlayerStateBehavior_Dribble_Stay.getInstance()))
		{
			onSuccess(owner);
		}
		else if (owner.player.paraPlayer.IsOffense())
		{
			owner.do_defense_defense();
		}
		else
		{
			if (owner.player.isInState(PlayerStateBehavior_Rebound.getInstance()))
			{
				return;
			}
			if (Instance<Ball>.instance.isInState(BallState_Rebound.getInstance()) && Instance<Ball>.instance.isGetBall(owner.player.getPosition()))
			{
				Vector3 position = Instance<Ball>.instance.getPosition();
				if (position.y > 2.5f)
				{
					owner.player.do_rebound();
					return;
				}
				Vector3 position2 = Instance<Ball>.instance.getPosition();
				if (position2.y < 1.8f)
				{
					onSuccess(owner);
					if (owner.player.teamType == TeamType.USER)
					{
						Instance<Court>.instance.count_PauseData_User[7]++;
					}
					else
					{
						Instance<Court>.instance.count_PauseData_Com[7]++;
					}
				}
			}
			else if (MathHelper.getSqrDistance(MathHelper.setY(Instance<Ball>.instance.getPosition(), 0f), owner.player.getOffenseBasketPos()) < MathHelper.getSqrDistance(owner.player.getPosition(), owner.player.getOffenseBasketPos()) || Instance<Ball>.instance.isInState(BallState_Rebound.getInstance()))
			{
				GameHelper.MoveTo(owner.player, MathHelper.setY(Instance<Ball>.instance.getPosition(), 0f), owner.player.getMoveSpeed(), isLookAtTargetPos: true);
				owner.player.do_run();
			}
			else
			{
				owner.player.do_idle();
				owner.time_Stop = 0.5f;
			}
		}
	}

	private void onSuccess(PlayerAI owner)
	{
		Instance<Court>.instance.OnGrabedRebound(owner.player);
		if (owner.player.teamType != 0)
		{
			if (Vector3.SqrMagnitude(owner.player.getPosition() - owner.player.getOffenseBasketPos()) < 0.5f)
			{
				owner.turnDirection();
				owner.do_offense_dribble();
			}
			else if (MathHelper.isInPercent(25f) && Vector3.SqrMagnitude(owner.player.getPosition() - owner.player.getOffenseBasketPos()) > 1f && Vector3.SqrMagnitude(owner.player.getPosition() - owner.player.getOffenseBasketPos()) < 30f)
			{
				owner.do_offense_shoot();
			}
			else
			{
				owner.turnDirection();
				owner.do_offense_dribble();
			}
		}
	}
}
